// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import Login from '../views/Login.vue';
import DashboardLayout from '../views/Layout.vue'; // 导入布局组件
import Dashboard from '../views/Dashboard.vue';
import UserManage from '../views/UserManage.vue';
import CourseManage from '../views/CourseManage.vue';
import ChapterManage from '../views/ChapterManage.vue';
import MaterialManage from '../views/MaterialManage.vue';
import Profile from '../views/Profile.vue';

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            component: Login,
            meta: { requiresAuth: false }
        },
        {
            path: '/login',
            component: Login,
            meta: { requiresAuth: false },
            name: '登录页'
        },
        {
            path: '/dashboard',
            component: DashboardLayout, // 使用布局组件
            meta: { requiresAuth: true },
            children: [
                { path: '', component: Dashboard, name: '系统概览' }, // 默认子路由
                { path: 'user-manage', component: UserManage, name: '用户管理' },
                { path: 'course-manage', component: CourseManage, name: '课程管理' },
                { path: 'chapter-manage', component: ChapterManage, name: '章节管理' },
                { path: 'material-manage', component: MaterialManage, name: '资料管理' },
                { path: 'profile', component: Profile, name: '个人中心' }
            ]
        },
        { path: '/:pathMatch(.*)*', redirect: '/' }
    ]
});

// 路由守卫保持不变
router.beforeEach((to, from, next) => {
    const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
    if (to.meta.requiresAuth && !isLoggedIn) {
        next('/');
    } else {
        next();
    }
});

export default router;